Freshers / Beginner level questions
Freshers / Beginner level questions & answers
Ques 1. What is the purpose of the 'super' keyword in Java?
The 'super' keyword in Java is used to refer to the immediate parent class object. It is often used to call the parent class's methods, access its fields, and invoke the parent class's constructor.
Example:
Example:
class Subclass extends Superclass {
void display() {
super.display(); // calls the display method of the parent class
}
}
Ques 2. What is the 'this' keyword in Java?
'this' refers to the current instance of the class. It is used to differentiate instance variables from local variables when they have the same name. 'this' is also used to invoke current class methods and constructors.
Example:
Example:
public class MyClass {
int x;
void setX(int x) {
this.x = x; // sets the instance variable x
}
}
Ques 3. Explain the concept of method overloading in Java.
Method overloading allows a class to have multiple methods with the same name but different parameters. The compiler determines which method to call based on the number and types of arguments passed.
Example:
Example:
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Ques 4. Explain the 'instanceof' operator in Java.
The 'instanceof' operator is used to test if an object is an instance of a particular class or interface. It returns a boolean value: 'true' if the object is an instance, and 'false' otherwise.
Example:
Example:
if (myObject instanceof MyClass) {
// do something
}
Ques 5. What is the purpose of the 'break' statement in Java?
The 'break' statement is used to terminate the loop or switch statement. It is often used to exit a loop prematurely when a certain condition is met.
Example:
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // exit the loop when i is 5
}
}
Ques 6. Explain the 'String' class and why it is immutable in Java.
The 'String' class in Java represents a sequence of characters. It is immutable to enhance performance and security. Once a 'String' object is created, its value cannot be changed. Any operation that appears to modify the 'String' actually creates a new 'String' object.
Example:
Example:
String str = 'Hello';
str = str.concat(' World'); // creates a new String object
Ques 7. What is the 'java.lang.Math' class in Java, and how is it used?
'java.lang.Math' is a utility class in Java that provides mathematical functions. It includes methods for basic arithmetic, trigonometry, logarithms, exponentiation, and more. The methods are static and can be used without creating an instance of the 'Math' class.
Example:
Example:
double result = Math.sqrt(25.0); // calculates the square root
Ques 8. What is the 'Java Virtual Machine (JVM)' and its role in Java?
The JVM is a virtual machine that enables a computer to run Java programs. It interprets Java bytecode and translates it into machine code for the host system. The JVM provides platform independence, as Java programs can run on any device that has a compatible JVM installed.
Example:
No specific example, as the JVM itself is not directly programmable.
Most helpful rated by users: